/**
 * Created by jiangqifan on 2017/6/20.
 */
(function() {
    ADF.actionModel.BottomModel = function() {
        ADF.actionModel.BottomModel.superclass.constructor.call(this);
        this.config = {text:"置底",icon:"icon-zhidi"};
        this.id = "bottom";
    };

    COM.util.defineClass(ADF.actionModel.BottomModel, AGEF.ActionModel, {
        execute: function(graphicalViewer){
            var selections = graphicalViewer.getSelection();
            var cmd = COM.gef.Command.createCommand({
                child: selections[0].model,
                originIndex: -1,
                execute: function() {
                  var child = this.child;
                  var parent = child.parent;

                  var children = parent.getPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN);
                  var index = children.indexOf(child);
                  this.originIndex = index;
                  var newChildren = children.slice(0, children.length);
                  newChildren.splice(index,1);
                  newChildren.unshift(child);
                  parent.setPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN, newChildren);
                },

                undo: function() {
                  var child = this.child;
                  var parent = child.parent;

                  var children = parent.getPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN);
                  var newChildren = children.slice(0, children.length);
                  newChildren.shift();
                  newChildren.splice(this.originIndex,0, child);
                  parent.setPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN, newChildren);
                }
            });
            var domain = graphicalViewer.getEditDomain();
            var stack = domain.getCommandStack();
            stack.execute(cmd);
        },
        getEnabledState : function(graphicalViewer){
            var selections = graphicalViewer.getSelection();
            if(!selections){
              return false;
            }
            //Diagram不允许调整顺序
            if(selections.length == 1 && COM.util.isInstanceOf(selections[0], ADF.editpart.DiagramEditPart)){
              return false;
            }
            //TODO 判断一下布局?
            return selections.length == 1;
        }
    });
})();
